package cn.sykj.car.service.impl;

import cn.sykj.car.dict.CarStoreCheckFlagEnum;
import cn.sykj.car.common.Const;
import cn.sykj.car.common.StatusCode;
import cn.sykj.car.entity.CarStore;
import cn.sykj.car.exception.CommonException;
import cn.sykj.car.exception.DictionaryDictException;
import cn.sykj.car.exception.ParameterInvalidException;
import cn.sykj.car.mapper.CarStoreMapper;
import cn.sykj.car.service.ICarStoreService;
import cn.sykj.car.utils.DiffEntityUtils;
import cn.sykj.car.utils.Utils;
import cn.sykj.car.vo.CarStoreVO;
import cn.sykj.car.vo.ResponseMessageVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 门店信息 服务实现类
 * </p>
 *
 * @author xuming
 * @since 2021-06-17
 */
@Service
public class CarStoreServiceImpl extends ServiceImpl<CarStoreMapper, CarStore> implements ICarStoreService {

    /**
     * 添加店铺
     *
     * @param carStore
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public ResponseMessageVO addCarStore(CarStore carStore) throws Exception {

        // 判断参数是否为空
        if (Utils.isEmpty(carStore)) {
            throw new ParameterInvalidException();
        }

        // 判断省不能为空，
        if (Utils.isEmpty(carStore.getProvince())) {
            throw new CommonException("", "省名称不能为空");
        }

        // 判断市不能为空，
        if (Utils.isEmpty(carStore.getCity())) {
            throw new CommonException("", "市名称不能为空");
        }

        // 判断填写门店不能为空,
        if (Utils.isEmpty(carStore.getCounty())) {
            throw new CommonException("", "区县名称不能为空");
        }

        // 拼接省，市，区编码生成门店编码
        if (Utils.notEmpty(carStore.getProvince()) && Utils.notEmpty(carStore.getCounty())
                && Utils.notEmpty(carStore.getCity())) {
            long maxStoreNo = baseMapper.selectMaxStoreNo(carStore);
            carStore.setStoreNo(Long.toString(maxStoreNo));
        }

        boolean ret = this.save(carStore);

        ResponseMessageVO responseMessageVO = new ResponseMessageVO();
        responseMessageVO.setRetType("添加门店");
        responseMessageVO.setRetFlag(ret);
        responseMessageVO.setRetMsg("添加门店，ID为：" + carStore.getId() + ";" + "添加门店名称:" + carStore.getStoreName());
        return responseMessageVO;

    }

    /**
     * 修改门店信息
     *
     * @param carStore
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public ResponseMessageVO updateCarStore(CarStore carStore) throws Exception {

        // 验证参数是否为空
        if (Utils.isEmpty(carStore) || Utils.isEmpty(carStore.getId())) {
            throw new ParameterInvalidException();
        }

        // 判断原始记录是否存在
        CarStore o_carStore = this.getById(carStore.getId());
        if (Utils.isEmpty(o_carStore) || o_carStore.getDelFlag().equals(Const.DEL_FLAG_TRUE)) {
            throw new DictionaryDictException("", "记录不存在");
        }

        // 把要更新的对象中的属性，覆盖到已有对象中
        boolean ret = baseMapper.updateCarStore(carStore) > 0;

        // 定义修改字符串：
        String editMes = "";

        // 遍历修改的内容与原始内容相比较，如果有变更，则记录
        CarStore afterCarStore = this.getById(carStore.getId());
        Map<String, List<Object>> diffMap = DiffEntityUtils.getDifferent(o_carStore, afterCarStore);
        if (Utils.notEmpty(diffMap) && diffMap.size() > 0) {
            for (String s : diffMap.keySet()) {
                editMes = editMes + ">" + s + "==>修改前【" + diffMap.get(s).get(0) + "】-修改后【" + diffMap.get(s).get(1) + "】;";
            }
        }

        ResponseMessageVO responseMessageVO = new ResponseMessageVO();
        responseMessageVO.setRetType("修改门店");
        responseMessageVO.setRetFlag(ret);
        responseMessageVO.setRetMsg("修改门店，ID为：" + afterCarStore.getId() + ";具体修改信息为：" + editMes);
        return responseMessageVO;

    }

    /**
     * 删除门店信息
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public ResponseMessageVO delete(Integer id) throws Exception {

        // 判断参数是否为空
        if (Utils.isEmpty(id)) {
            throw new ParameterInvalidException();
        }

        // 判断参数是否存在
        CarStore carStore = this.getById(id);
        if (Utils.isEmpty(carStore)) {
            throw new CommonException(StatusCode.CAR_STORE_NOT_FOUND.getCode(),
                    StatusCode.CAR_STORE_NOT_FOUND.getMsg());
        }

        boolean ret = this.removeById(id);
        ResponseMessageVO responseMessageVO = new ResponseMessageVO();
        responseMessageVO.setRetType("删除门店");
        responseMessageVO.setRetFlag(ret);
        responseMessageVO.setRetMsg("删除门店，ID为:" + carStore.getId() + ";" + "删除门店名:" + carStore.getStoreName());
        return responseMessageVO;

    }

    /**
     * 门店信息查询
     *
     * @param page
     * @param carStoreVO
     * @return
     * @throws Exception
     */
    @Override
    public IPage<CarStoreVO> pageCarStore(IPage<CarStoreVO> page, CarStoreVO carStoreVO) {

        List<CarStoreVO> list = baseMapper.listCarStore(page, carStoreVO);
        for (int i = 0; i < list.size(); i++) {
            CarStoreVO carStoreVO1 = list.get(i);
            if (Utils.notEmpty(carStoreVO1) && Utils.notEmpty(carStoreVO1.getCheckFlag())) {
                // 审批：1-申请 2-审批  3-驳回
                if (carStoreVO1.getCheckFlag().intValue() == CarStoreCheckFlagEnum.APPLY.VALUE) {
                    carStoreVO1.setCheckFlagName(CarStoreCheckFlagEnum.APPLY.NAME);
                }
                if (carStoreVO1.getCheckFlag().intValue() == CarStoreCheckFlagEnum.CHECK.VALUE) {
                    carStoreVO1.setCheckFlagName(CarStoreCheckFlagEnum.CHECK.NAME);
                }
                if (carStoreVO1.getCheckFlag().intValue() == CarStoreCheckFlagEnum.REFUND.VALUE){
                    carStoreVO1.setCheckFlagName(CarStoreCheckFlagEnum.REFUND.NAME);
                }
            }
        }
        page.setRecords(list);
        return page;

    }

    /**
     * 审核门店信息
     *
     * @param carStoreVO
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public ResponseMessageVO checkCarStore(CarStoreVO carStoreVO) throws Exception {

        //判断参数是否为空
        if (Utils.isEmpty(carStoreVO)) {
            throw new ParameterInvalidException();
        }

        //通过id获取原始记录
        CarStore o_carStore = this.getById(carStoreVO.getId());
        if (Utils.isEmpty(o_carStore)) {
            throw new CommonException(StatusCode.CAR_STORE_NOT_FOUND.getCode(),
                    StatusCode.CAR_STORE_NOT_FOUND.getMsg());
        }
        if (o_carStore.getCheckFlag().intValue() == CarStoreCheckFlagEnum.CHECK.VALUE
                || o_carStore.getCheckFlag().intValue() == CarStoreCheckFlagEnum.REFUND.VALUE) {
            throw new CommonException(StatusCode.CAR_STORE_CHECKED.getCode(),
                    StatusCode.CAR_STORE_CHECKED.getMsg());
        }

        //修改car_store表
        CarStore carStore = this.getById(carStoreVO.getId());
        carStore.setCheckFlag(carStoreVO.getCheckFlag());
        carStore.setCheckTime(new Date());
        carStore.setCheckUser(carStoreVO.getCheckUser());
        carStore.setStoreFee(carStoreVO.getStoreFee());

        boolean ret = this.updateById(carStore);
        if (!ret) {
            throw new ParameterInvalidException();
        }

        // 定义修改字符串：
        String editMes = "";

        // 遍历修改的内容与原始内容相比较，如果有变更，则记录
        CarStore afterCarStore = this.getById(carStoreVO.getId());
        Map<String, List<Object>> diffMap = DiffEntityUtils.getDifferent(o_carStore, afterCarStore);
        if (Utils.notEmpty(diffMap) && diffMap.size() > 0) {
            for (String s : diffMap.keySet()) {
                editMes = editMes + ">" + s + "==>修改前【" + diffMap.get(s).get(0) + "】-修改后【" + diffMap.get(s).get(1) + "】;";
            }
        }

        ResponseMessageVO responseMessageVO = new ResponseMessageVO();
        responseMessageVO.setRetType("审核门店");
        responseMessageVO.setRetFlag(ret);
        responseMessageVO.setRetMsg("审核门店，ID为：" + afterCarStore.getId() + ";具体修改信息为：" + editMes);
        return responseMessageVO;
    }

    /**
     * 导出门店信息
     *
     * @param carStoreVO@return
     * @throws Exception
     */
    @Override
    public List<CarStoreVO> exportCarStore(CarStoreVO carStoreVO) {

        List<CarStoreVO> carStoreList = baseMapper.listCarStore(carStoreVO);

        for (int i = 0; i < carStoreList.size(); i++) {
            CarStoreVO carStoreVO1 = carStoreList.get(i);
            if (Utils.notEmpty(carStoreVO1) && Utils.notEmpty(carStoreVO1.getCheckFlag())) {
                // 审批：1-申请 2-审批  3-驳回
                if (carStoreVO1.getCheckFlag().intValue() == CarStoreCheckFlagEnum.APPLY.VALUE) {
                    carStoreVO1.setCheckFlagName(CarStoreCheckFlagEnum.APPLY.NAME);
                }
                if (carStoreVO1.getCheckFlag().intValue() == CarStoreCheckFlagEnum.CHECK.VALUE) {
                    carStoreVO1.setCheckFlagName(CarStoreCheckFlagEnum.CHECK.NAME);
                }
                if (carStoreVO1.getCheckFlag().intValue() == CarStoreCheckFlagEnum.REFUND.VALUE){
                    carStoreVO1.setCheckFlagName(CarStoreCheckFlagEnum.REFUND.NAME);
                }
            }
        }
        return carStoreList;

    }
}
